package br.igk.isnmp.util;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import br.igk.isnmp.core.SNMPBadValueException;
import br.igk.isnmp.core.SNMPGetException;
import br.igk.isnmp.core.SNMPSequence;
import br.igk.isnmp.core.SNMPVarBindList;
import br.igk.isnmp.core.SNMPv1CommunicationInterface;
import br.igk.isnmp.exception.BusinessException;

public class SNMPConnection {
	private SNMPv1CommunicationInterface comInterface = null;
	private int version;
	private InetAddress hostAddress;
	private String community;

	public SNMPConnection(int version, InetAddress hostAddress, String community) throws BusinessException {
		try {
			this.version = version;
			this.hostAddress = hostAddress;
			this.community = community;
			comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
			comInterface.setReceiveBufferSize(10000);
		} catch (Throwable e) {
			throw new BusinessException(Thread.currentThread().getName() + " ERRO: ", e.getMessage());
		}
	}


	public Map<String, SNMPEntry> getEntry(String[] itemID) throws BusinessException {
		Map<String, SNMPEntry> result = new HashMap<String, SNMPEntry>();

		try {
			for (String item : itemID) {
				SNMPEntry entry = new SNMPEntry(getMIBEntry(item));
				result.put(item, entry);
			}
		} catch (Throwable e) {
			throw new BusinessException(Thread.currentThread().getName() + " ERRO", e.getMessage());
		}

		return result;
	}


	public Map<String, SNMPEntry> getTableEntry(String baseID) throws BusinessException {
		Map<String, SNMPEntry> result = new HashMap<String, SNMPEntry>();

		try {
			SNMPVarBindList tableVars = comInterface.retrieveMIBTable(baseID);

			for (int i = 0; i < tableVars.size(); i++) {
				SNMPEntry entry = new SNMPEntry((SNMPSequence)tableVars.getSNMPObjectAt(i));
				result.put(entry.getSnmpOID().toString(), entry);
			}
		} catch (Throwable e) {
			throw new BusinessException(Thread.currentThread().getName() + " ERRO", e.getMessage());
		}

		return result;
	}


	public SNMPEntry getEntry(String itemID) throws BusinessException {
		try {
			return new SNMPEntry(getMIBEntry(itemID));
		} catch (Throwable e) {
			throw new BusinessException(Thread.currentThread().getName() + " ERRO", e.getMessage());
		}
	}


	public SNMPSequence getMIBEntry(String itemID) throws SNMPGetException, IOException, SNMPBadValueException {
		SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
		return (SNMPSequence)(newVars.getSNMPObjectAt(0));
	}


	public void close() throws BusinessException {
		try {
			if (comInterface != null)
				comInterface.closeConnection();
		} catch (Throwable e) {
			throw new BusinessException(Thread.currentThread().getName() + " ERRO", e.getMessage());
		}
	}


	public int getVersion() {
		return version;
	}


	public void setVersion(int version) {
		this.version = version;
	}


	public InetAddress getHostAddress() {
		return hostAddress;
	}


	public void setHostAddress(InetAddress hostAddress) {
		this.hostAddress = hostAddress;
	}


	public String getCommunity() {
		return community;
	}


	public void setCommunity(String community) {
		this.community = community;
	}


	public SNMPv1CommunicationInterface getComInterface() {
		return comInterface;
	}
}
